using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics.Contracts;
using Commons;
using Datas;
using UnityEngine;
using Mgrs;
using MydisDatas;
using UnityEngine.SceneManagement;
using UnityEngine.EventSystems;
using UnityEngine.Playables;
using UnityEngine.UI;

public class CurePatientController : ComMonoBehaviour
{
    //按钮相关
    public Button winContinueButton;
    public Button loseContinueButton;
    public Button medicalKit;
    public Button GiveUpButton;//放弃治疗按钮
    //出牌按钮
    public Button theOutHandButton;
    
    //
    public GameObject conversation;
    //回合控制相关
    public bool isYourTurn;
    public int yourTurn;
    //public Text turnText;
    public int turnCount;
    public Text turnNumberText;
    public Button endTurnButton;
    
    public Text YouHaveTurnNumText;
    public int YouHaveTurnNum;
    public GameObject losePanel;

    public GameObject ToMyHandCardPrefab;//手卡的预制体

    public GameObject QuestionCardPre;//询问牌预制体

    private GameObject _questionCard;//询问牌

    public GameObject HandPlace;

    //定义初始手卡的数组，长度为5
    public GameObject[] handCardNum = new GameObject[5];
    //定义选定的手卡的空数组，长度也为5
    public GameObject[] selectedHandCards = new GameObject[5];
    //游戏胜利失败场景判定
    public bool isWin;

    public bool isLose;
    //赢与输通道
    public GameObject theWinPanel;
    public GameObject theLosePanel;
    //交换按钮
    public Button theExchangeButton;
    //测试牌包用的
    public List<int> AllCardList;
    //定义第一个病人
    //private List<int> FirstPatient = new List<int>();
    public void Awake()
    {
        //Instantiate(ThePatientPre, PatientPlace.transform);
        AllCardList =PlayerData.GetCardIds();
        PlayerData.AddCardId(0);
        PlayerData.AddCardId(1);
        PlayerData.AddCardId(2);
        PlayerData.AddCardId(3);
        PlayerData.AddCardId(4);
        PlayerData.AddCardId(4);
        
        
        for (int i=0; i < 5; i++)
        {
            handCardNum[i] = Instantiate(ToMyHandCardPrefab,HandPlace.transform);
            //handCardNum[i].transform.parent = HandPlace.transform;
        }
        _questionCard = Instantiate(QuestionCardPre,HandPlace.transform);
    }
    // Start is called before the first frame update
    void Start()
    {
        RegisterMsg(MsgData.haveSelectHandCard, o => CheckOutHandButtonActive());
        RegisterMsg(MsgData.haveNoSelectHandCard, o => CheckOutHandButtonNotActive());
        RegisterMsg(MsgData.BackSlotId,ChangeCard);
        //继续游戏按钮
        winContinueButton.onClick.AddListener(CtnBtnDown);
        loseContinueButton.onClick.AddListener(CtnBtnDown);
        //药箱按钮
        medicalKit.onClick.AddListener(MdcKitBtnDown);
        //回合结束按钮
        endTurnButton.onClick.AddListener(EndYourTurn);
        //放弃治疗按钮
        GiveUpButton.onClick.AddListener(GUBDown);
        isYourTurn = true;
        turnCount = 1;
        //交换手牌按钮
        //theExchangeButton.onClick.AddListener(ChangeCard(List<int>,List<int>));
        //手牌区域
        HandPlace = GameObject.Find("HandPlace");
        
        //监听出牌按钮
        theOutHandButton.onClick.AddListener(CheckTheSquenceOfCards);
        
    }

    private void OnDisable()
    {
        HandCardMgr.ClearAllData();
    }

    // Update is called once per frame
    void Update()
    {
        if (isYourTurn == true)
        {
            //turnText.text = "Your Turn";
            
        }
        
        turnNumberText.text = "当前回合数：" + turnCount;

        if (isWin==true)
        {
            theWinPanel.SetActive(true);
            isLose = false;
            theLosePanel.SetActive(false);
        }else if (isLose == true)
        {
            theWinPanel.SetActive(false);
            isWin = false;
            theLosePanel.SetActive(true);
        }

        YouHaveTurnNum = ThisPatient.theMaxTurn;
        YouHaveTurnNumText.text = "最大回合数：" + YouHaveTurnNum;
        
    }
    //换牌按钮事件
    public void ChangeCard(object index)
    {
        Debug.Log("交换成功"+index);

        // for (int i = 0; i <= HandCardMgr.CurSeleteIndexs.Count; i++)
        // {
            var cardId = PlayerData.GetCardIds()[(int) index];
            PlayerData.RemoveCardId(cardId);

            var gameObject = HandCardMgr.CurSeleteIndexs[0];
            
            Debug.Log(gameObject);
            int beforeId = gameObject.GetComponent<ThisHandCard>().id;
            PlayerData.AddCardId(beforeId);
            
            gameObject.GetComponent<ThisHandCard>().ChangeId(cardId);

            HandCardMgr.CancelSelectCardId(beforeId);
            HandCardMgr.SelectCardId(cardId);
            
            //}



    }
    //回合结束按钮
    public void EndYourTurn()
    {
       //  bool handCardHaveSelect=ThisHandCard.
        int theMaxTurn;
        theMaxTurn = ThisPatient.theMaxTurn;
        var curQuestioncardState = HandCardMgr.haveSelectQuestionCard;
        //如果超过最大回合数则输掉游戏
        if (turnCount+1 > theMaxTurn)
        {
            isLose = true;
            //turnCount = theMaxTurn;
            Destroy(turnNumberText);
        }
        
        var curSeleteIndexs = HandCardMgr.CurSeleteIndexs;
        isYourTurn = false;
        //ThisHandCard.isSelect = false;
        for (int i = 0; i <curSeleteIndexs.Count; i++)
        {
            curSeleteIndexs.Remove(curSeleteIndexs[i]);
        }
        //theOutHandButton.gameObject.SetActive(false);
        turnCount += 1;
        if (curQuestioncardState.Count==0)
        {
            _questionCard.SetActive(true);
        }
        
    }
    //继续游戏按钮
    public void CtnBtnDown()
    {
        Debug.Log("结束游戏");
        SceneManager.LoadScene("MainScene");
        var selectQuestioncard = QuestionCard.isSelect;
        var curQuestioncardState = HandCardMgr.haveSelectQuestionCard;
        var curSelectcardState = HandCardMgr.CurSelectedHandCardsId;
        selectQuestioncard = false;
        curQuestioncardState.Clear();
        curSelectcardState.Clear();
    }
    
    //药箱按钮
    public void MdcKitBtnDown()
    {
        PanelMgr.OpenKitPanel(transform);
    }
    //放弃治疗按钮事件
    public void GUBDown()
    {
        isLose = true;
        PlayerData.ChangePrestige(-10);
        isWin = false;
        // if (isWin)
        // {
        //     PlayerData.ChangeMoney(100);
        //     PlayerData.ChangePrestige(10);
        // }
        // else
        // {
        //     PlayerData.ChangePrestige(-10);
        // }
    }
    
    //判断是否还有选中手牌
    public void CheckOutHandButtonActive()
    {
        //var selectQuestioncard = QuestionCard.isSelect;
        var curQuestioncardState = HandCardMgr.haveSelectQuestionCard;
        var curSeleteIndexs = HandCardMgr.CurSeleteIndexs;
       // Debug.Log(selectQuestioncard);
        if(curSeleteIndexs.Count > 0 || curQuestioncardState.Count>0)//如果当前有选中的手卡或者选中了询问卡
            theOutHandButton.gameObject.SetActive(true);
    }

    public void CheckOutHandButtonNotActive()
    {
        //var selectQuestioncard = QuestionCard.isSelect;
        var curQuestioncardState = HandCardMgr.haveSelectQuestionCard;
        var curSeleteIndexs = HandCardMgr.CurSeleteIndexs;
        if(curSeleteIndexs.Count == 0 && curQuestioncardState.Count==0)//如果当前没有选中的手卡以及没有选中询问卡
            theOutHandButton.gameObject.SetActive(false);
    }
    
    
    /**出牌按钮事件*/
    public void CheckTheSquenceOfCards()
    {
        var selectQuestioncard = QuestionCard.isSelect;
        var curQuestioncardState = HandCardMgr.haveSelectQuestionCard;
        //判断所选卡牌数组与病人所需数组
        var HandCardSquence = HandCardMgr.CurSelectedHandCardsId;
        /**病人需要的牌序*/
        var thisPatientNeedThese = ThisPatient.thisPatientNeeds;
        if (ListEquals(HandCardSquence, thisPatientNeedThese) == true && selectQuestioncard == false)
        {
            isWin = true;
            theOutHandButton.gameObject.SetActive(false);
            PlayerData.ChangeMoney(100);
            PlayerData.ChangePrestige(10);
        }
        else if(ListEquals(HandCardSquence, thisPatientNeedThese) == false && selectQuestioncard==false)
        {
            isLose = true;
            theOutHandButton.gameObject.SetActive(false);
            PlayerData.ChangePrestige(-10);
        } else if (selectQuestioncard == true&& HandCardSquence.Count==0)//如果只选中了询问牌后点击了出牌
        {
            var promp= Instantiate(conversation,transform);
            promp.GetComponent<PatientDialog>().GetPatienDialogText(ThisPatient.Patientid);
            _questionCard.SetActive(false);
            QuestionCard.isSelect = false;
            HandCardMgr.CancelSelectQuestionCard(_questionCard);
            theOutHandButton.gameObject.SetActive(false);
            //curQuestioncardState.Remove(0);
            //发出询问消息，病人接收到后给出对话
        }
    }
    //判断两个List是否相等
    private bool ListEquals<T>(List<T> list1,List<T> list2)
    {
        return new HashSet<T>(list1).SetEquals(list2);
    }

    protected override void OnBeforeDestroy()
    {
        
    }
}
